﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Common.Net.DataStructures
{
    public class ValueContainer<T> : IEquatable<ValueContainer<T>>
    {
        private readonly T _value;

        public T Value
        {
            get { return _value; }
        }

        public ValueContainer(T value)
        {
            _value = value;
        }

        public override string ToString()
        {
            return Value == null ? "(null)" : Value.ToString();
        }

        #region Equals, GetHashCode, operator ==, etc.

        public bool Equals(ValueContainer<T> other)
        {
            if (other == null)
                return false;

            if (this.GetType() != other.GetType())
                return false;

            return object.Equals(this.Value, other.Value);
        }

        public override bool Equals(object obj)
        {
            if (obj == null)
                return false;

            if (this.GetType() != obj.GetType())
                return false;

            return Equals((ValueContainer<T>)obj);
        }

        public static bool operator ==(ValueContainer<T> left, ValueContainer<T> right)
        {
            return object.Equals(left, right);
        }

        public static bool operator !=(ValueContainer<T> left, ValueContainer<T> right)
        {
            return !(left == right);
        }

        public override int GetHashCode()
        {
            return Value == null ? 0 : Value.GetHashCode();
        }

        #endregion
    }

    public class ValueContainer
    {
        public static ValueContainer<T> Create<T>(T value)
        {
            return new ValueContainer<T>(value);
        }
    }
}
